/*
 * This is GPL v2 licensed code. See the file LICENSE in the
 * source distribution, or www.gnu.org/copyleft/gpl.html, for
 * more information.
 *
 * Copyright (C) by Håkan Lindqvist 2006, 2007
 */

package PolicyAlgebra.Controller;


/** 
 * This class is the algebra's point of central control; settings
 * and meta information is fetched using this class.
 */
public class Controller {
	private static Controller controllerSingleton = new Controller();
	private String installationLocation;

	/** 
	 * Private constructor.
	 */
	private Controller() {
		/* Set the location of the library and tools */
		configInstallationLocation();
	}


	/** 
	 * The method is a helper method to the constructor and sets the
	 * 'libraryLocation' member properly.
	 */
	private void configInstallationLocation() {
		/* Set path to the algebra's components */
		String sep = System.getProperty("file.separator");
		installationLocation = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

		/* Two steps up is the parent directory for the algebra */
		installationLocation = installationLocation.substring(0, installationLocation.lastIndexOf(sep));
	}


	/** 
	 * Access method for the controller singleton. 
	 * 
	 * @return The instance of the controller that is available.
	 */
	public static Controller getInstance() {
		return controllerSingleton;
	}


	/** 
	 * Getter for the installationLocation member. 
	 * 
	 * @return The location of the policyalgebra's installation.
	 */
	public String getInstallationLocation() {
		return installationLocation;
	}


	/** 
	 * Resolves the name of the file which can be used with a classloader to
	 * enable instanciation of a plugin.
	 * 
	 * @param pluginName The name of the plugin to resolv file name for
	 *        (using the plugin configuration).
	 * @return The filename that should be used in conjunction with a classloader.
	 * @throws ControllerException If no filename corresponding to the supplied
	 *         name is found.
	 */
	public String getPluginFileName(String pluginName) throws ControllerException {
		return PluginController.getInstance().getPluginFileName(pluginName);
	}


	/** 
	 * Resolves the class name of the file which can be used with a classloader to
	 * enable instanciation of a plugin.
	 * 
	 * @param pluginName The name of the plugin to resolv file name for
	 *        (using the plugin configuration).
	 * @return The class name that should be used in conjunction with a classloader.
	 * @throws ControllerException If no class name corresponding to the supplied
	 *         name is found.
	 */
	public String getPluginClassName(String pluginName) throws ControllerException {
		return PluginController.getInstance().getPluginClassName(pluginName);
	}
}
